# delimit ;
capture log close;
set more 1;  
clear; 
set mem 300m; 
local divide 2; 
* set this to 1 if include 2020 wave;
global INCLUDE2020 1;

*global folder "C:\research";
*global folder "C:\Users\Jeremy\Dropbox";
*global folder "~/";
global folder "D:\Dropbox\";
*global folder "\\econ-san-fs\Home4\uctpkp0\SystemFolder\Windows7\Desktop";
*global folder "\\ad.ucl.ac.uk\homer\zctpmer\downloads";



         local saved  "$folder\hrs\merge\";
         local trackd "$folder\hrs\tracker\";
         local wave1d "$folder\hrs\wave1\";
         local wave2d "$folder\hrs\wave2\";
         local wave3d "$folder\hrs\wave3\";
         local wave4d "$folder\hrs\wave4\";
         local wave5d "$folder\hrs\wave5\";
		 local wave6d "$folder\hrs\wave6\";
         local wave7d "$folder\hrs\wave7\";
         local wave8d "$folder\hrs\wave8\";
		 local wave9d "$folder\hrs\wave9\";
		 local wave10d "$folder\hrs\wave10\";
		 local wave11d "$folder\hrs\wave11\";
		 local wave12d "$folder\hrs\wave12\";
		 local wave13d "$folder\hrs\wave13\";
	 	 local wave14d "$folder\hrs\wave14\";
		 local wave15d "$folder\hrs\wave15\";
         local aheadw1 "$folder\hrs\aheadw1\";
         local aheadw2 "$folder\hrs\aheadw2\";


log using `saved'healstat.log, replace ;

*wave14;
use `wave14d'H18C_R;
gen temp=real(hhid);
drop hhid;
gen HHID=temp;
drop temp;
gen temp=real(pn);
drop pn;
gen PN=temp;
drop temp;
sort HHID PN;
rename QC001 heal14;
rename QC002 healch14;
rename QC105 pain14;
replace pain14=0 if QC104==5;
keep HHID PN heal14 healch14 pain14;
save `saved'healstat, replace;
drop _all;

if $INCLUDE2020 {; 
disp "Doing 2020 processing..." ;
*wave15;
use `wave15d'H20C_R;
gen temp=real(hhid);
drop hhid;
gen HHID=temp;
drop temp;
gen temp=real(pn);
drop pn;
gen PN=temp;
drop temp;
sort HHID PN;
rename RC001 heal15;
rename RC002 healch15;
rename RC105 pain15;
replace pain15=0 if RC104==5;
keep HHID PN heal15 healch15 pain15;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;
};

*wave13;
use `wave13d'H16C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename PC001 heal13;
rename PC002 healch13;
rename PC105 pain13;
replace pain13=0 if PC104==5;
keep HHID PN heal13 healch13 pain13;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;


*wave12;
use `wave12d'H14C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename OC001 heal12;
rename OC002 healch12;
rename OC105 pain12;
replace pain12=0 if OC104==5;
keep HHID PN heal12 healch12 pain12;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;

*wave11;
use `wave11d'H12C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename NC001 heal11;
rename NC002 healch11;
rename NC105 pain11;
replace pain11=0 if NC104==5;
keep HHID PN heal11 healch11 pain1;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;



*wave10;
use `wave10d'H10C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename MC001 heal10;
rename MC002 healch10;
rename MC105 pain10;
replace pain10=0 if MC104==5;
keep HHID PN heal10 healch10 pain10;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;

*wave9;
use `wave9d'H08C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename LC001 heal9;
rename LC002 healch9;
rename LC105 pain9;
replace pain9=0 if LC104==5;
keep HHID PN heal9 healch9 pain9;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;

*wave8;
use `wave8d'H06C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename KC001 heal8;
rename KC002 healch8;
rename KC105 pain8;
replace pain8=0 if KC104==5;
keep HHID PN heal8 healch8 pain8;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;

*wave7;
use `wave7d'H04C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename JC001 heal7;
rename JC002 healch7;
rename JC105 pain7;
replace pain7=0 if JC104==5;
keep HHID PN heal7 healch7 pain7;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;

*wave6;
use `wave6d'H02C_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;
sort HHID PN;
rename HC001 heal6;
rename HC002 healch6;
rename HC105 pain6;
replace pain6=0 if HC104==5;
keep HHID PN heal6 healch6 pain6;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;


*merge wave5 files;
use `wave5d'H00B_R;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
rename G1226 Q1226;
rename Q1226 heal5;
*Q1226 - rate health;
rename G1229 Q1229;
rename Q1229 healch5;
rename G1374 pain5;
replace pain5=0 if G1372==5;
*Q1229 - rate health past;
keep HHID PN heal5 healch5 pain5;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;

*merge wave4 files;
use `wave4d'h98b_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp;

sort HHID PN;
rename F1097 Q1097;
rename Q1097 heal4;
*Q1097 - rate health;
rename F1100 Q1100;
rename Q1100 healch4;
*Q1100 - rate health past;
rename F1241 pain4;
replace pain4=0 if F1239==5;
keep HHID PN heal4 healch4 pain4;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN;
save `saved'healstat, replace;
drop _all;

*merge wave3 files;
use `wave3d'h96b_r;
gen temp=real(HHID);
drop HHID;
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp; 

rename E769 heal3;
rename E772 healch3;
rename E913 pain3;
replace pain3=0 if E911==5;
keep HHID PN heal3 healch3 pain3;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN; 
save `saved'healstat, replace;
drop _all;

*merge wave2 files;
use `wave2d'w2b;
gen temp=real(HHID);
drop HHID; 
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp; 
*gen HHIDPN=HHID*1000+PN;
rename W301 heal2;
rename W302 healch2;
rename W438 pain2;
replace pain2=0 if W437==5;
keep HHID PN heal2 healch2 pain2;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN; 
save `saved'healstat, replace;
drop _all;

*now merge wave1 files;
use `wave1d'health;
gen temp=real(HHID);
drop HHID; 
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp; 
rename V301 heal1;
rename V302 healch1;
rename V442 pain1;
replace pain1=0 if V440==5;
keep HHID PN heal1 healch1 pain1;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN; 
save `saved'healstat, replace;
drop _all;

*now merge ahead1 files;
use `aheadw1'Br21;
rename V204 aheal1;
rename V208 ahealch1;
gen temp=real(HHID);
drop HHID; 
gen HHID=temp;
drop temp;
gen temp=real(PN);
drop PN;
gen PN=temp;
drop temp; 
keep HHID PN aheal1 ahealch1;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;
sort HHID PN; 
save `saved'healstat, replace;
drop _all;

*now merge ahead2 files;
* figure out var names and dataset to be read from;
use `aheadw2'A95B_R;
gen temp=real(HHID);
     drop HHID;
     gen HHID=temp;
     drop temp;
     gen temp=real(PN);
     drop PN;
     gen PN=temp;
     drop temp;
rename D769 aheal2;
replace D772=D777 if D772==3;
* Ken: I don't understand what you were doing below;
*replace D772=3 if D772==2;
*replace D772=D776 if D772==1; 
rename D772 ahealch2;
rename D912 apain2;
replace apain2=0 if D911==5;
keep HHID PN aheal2 ahealch2 apain2;
sort HHID PN;
merge HHID PN using `saved'healstat;
drop _merge;

sort HHID PN; 
save `saved'healstat, replace;

* replace AHEAD data here;
replace heal1=aheal1 if heal1==.;
replace heal2=aheal2 if heal2==.;

sum *pain*;


replace pain2=apain2 if pain2==.;


* health status, (excellent, good, fair, poor);
gen ghealth1=.;
replace ghealth1=1 if heal1<4;
replace ghealth1=0 if heal1>3 & heal1~=.;
gen ghealth2=.;
replace ghealth2=1 if heal2<4;
replace ghealth2=0 if heal2>3 & heal2~=.;
gen ghealth3=.;
replace ghealth3=1 if heal3<4;
replace ghealth3=0 if heal3>3 & heal3~=.;
gen ghealth4=.;
replace ghealth4=1 if heal4<4;
replace ghealth4=0 if heal4>3 & heal4~=.;
gen ghealth5=.;
replace ghealth5=1 if heal5<4;
replace ghealth5=0 if heal5>3 & heal5~=.;
gen ghealth6=.;
replace ghealth6=1 if heal6<4;
replace ghealth6=0 if heal6>3 & heal6~=.;
gen ghealth7=.;
replace ghealth7=1 if heal7<4;
replace ghealth7=0 if heal7>3 & heal7~=.;
gen ghealth8=.;
replace ghealth8=1 if heal8<4;
replace ghealth8=0 if heal8>3 & heal8~=.;
gen ghealth9=.;
replace ghealth9=1 if heal9<4;
replace ghealth9=0 if heal9>3 & heal9~=.;
gen ghealth10=.;
replace ghealth10=1 if heal10<4;
replace ghealth10=0 if heal10>3 & heal10~=.;
gen ghealth11=.;
replace ghealth11=1 if heal11<4;
replace ghealth11=0 if heal11>3 & heal11~=.;
gen ghealth12=.;
replace ghealth12=1 if heal12<4;
replace ghealth12=0 if heal12>3 & heal12~=.;
gen ghealth13=.;
replace ghealth13=1 if heal13<4;
replace ghealth13=0 if heal13>3 & heal13~=.;
gen ghealth14=.;
replace ghealth14=1 if heal14<4;
replace ghealth14=0 if heal14>3 & heal14~=.;

if $INCLUDE2020 {; 
gen ghealth15=.;
replace ghealth15=1 if heal15<4;
replace ghealth15=0 if heal15>3 & heal15~=.;
};

tab healch1;
tab healch2;
tab healch3;
tab healch4;
tab healch5;
tab healch6;
tab healch7;
tab healch8;
tab healch9;
tab healch10;
tab healch11;
tab healch12;
tab healch13;

tab ahealch1;
tab ahealch2;

gen temp=.;
replace temp=0 if healch1==3;
replace temp=1 if healch1<3;
replace temp=-1 if healch1>3;
replace temp=. if healch1==0|healch1>5;
replace healch1=temp;

replace temp=.;
replace temp=0 if healch2==3;
replace temp=1 if healch2<3;
replace temp=-1 if healch2>3;
replace temp=. if healch2==0|healch2>5;
replace healch2=temp;

replace temp=.;
replace temp=0 if healch3==2;
replace temp=1 if healch3<2;
replace temp=-1 if healch3>2;
replace temp=. if healch3==0|healch3>5;
replace healch3=temp;

replace temp=.;
replace temp=0 if healch4==2;
replace temp=1 if healch4<2;
replace temp=-1 if healch4>2;
replace temp=. if healch4==0|healch4>5;
replace healch4=temp;

replace temp=.;
replace temp=0 if healch5==2;
replace temp=1 if healch5<2;
replace temp=-1 if healch5>2;
replace temp=. if healch5==0|healch5>5;
replace healch5=temp;

replace temp=.;
replace temp=0 if healch6==2;
replace temp=1 if healch6<2;
replace temp=-1 if healch6>2;
replace temp=. if healch6==0|healch6>5;
replace healch6=temp;

replace temp=.;
replace temp=0 if healch7==2;
replace temp=1 if healch7<2;
replace temp=-1 if healch7>2;
replace temp=. if healch7==0|healch7>5;
replace healch7=temp;

replace temp=.;
replace temp=0 if healch8==2;
replace temp=1 if healch8<2;
replace temp=-1 if healch8>2;
replace temp=. if healch8==0|healch8>5;
replace healch8=temp;

replace temp=.;
replace temp=0 if healch9==2;
replace temp=1 if healch9<2;
replace temp=-1 if healch9>2;
replace temp=. if healch9==0|healch9>5;
replace healch9=temp;

replace temp=.;
replace temp=0 if healch10==2;
replace temp=1 if healch10<2;
replace temp=-1 if healch10>2;
replace temp=. if healch10==0|healch10>5;
replace healch10=temp;

replace temp=.;
replace temp=0 if healch11==2;
replace temp=1 if healch11<2;
replace temp=-1 if healch11>2;
replace temp=. if healch11==0|healch11>5;
replace healch11=temp;

replace temp=.;
replace temp=0 if healch12==2;
replace temp=1 if healch12<2;
replace temp=-1 if healch12>2;
replace temp=. if healch12==0|healch12>5;
replace healch12=temp;

replace temp=.;
replace temp=0 if healch13==2;
replace temp=1 if healch13<2;
replace temp=-1 if healch13>2;
replace temp=. if healch13==0|healch13>5;
replace healch13=temp;

replace temp=.;
replace temp=0 if healch14==2;
replace temp=1 if healch14<2;
replace temp=-1 if healch14>2;
replace temp=. if healch14==0|healch14>5;
replace healch14=temp;

if $INCLUDE2020 {; 
replace temp=.;
replace temp=0 if healch15==2;
replace temp=1 if healch15<2;
replace temp=-1 if healch15>2;
replace temp=. if healch15==0|healch15>5;
replace healch15=temp;
};

replace temp=.;
replace temp=0 if ahealch1==3;
replace temp=1 if ahealch1<3;
replace temp=-1 if ahealch1>3;
replace temp=. if ahealch1==0|ahealch1>5;
replace ahealch1=temp;

replace temp=.;
replace temp=0 if ahealch2==2;
replace temp=1 if ahealch2<2;
replace temp=-1 if ahealch2>2;
replace temp=. if ahealch2==.|ahealch2==0|ahealch2>5;
replace ahealch2=temp;

tab healch1;
tab healch2;
tab healch3;
tab healch4;
tab healch5;
tab healch6;
tab healch7;
tab healch8;
tab healch9;
tab healch10;
tab healch11;
tab healch12;
tab healch13;

tab ahealch1;
tab ahealch2;

* replace AHEAD data here;
replace healch1=ahealch1 if healch1==.;
replace healch2=ahealch2 if healch2==.;



replace pain2=. if pain2> 3;
replace pain3=. if pain3> 3;
replace pain4=. if pain4> 3;
replace pain5=. if pain5> 3;
replace pain6=. if pain6> 3;
replace pain7=. if pain7> 3;
replace pain8=. if pain8> 3;
replace pain9=. if pain9> 3;
replace pain10=. if pain10> 3;
replace pain11=. if pain11> 3;
replace pain12=. if pain12> 3;
replace pain13=. if pain13> 3;
replace pain14=. if pain14> 3;
if $INCLUDE2020 {; 
replace pain15=. if pain15> 3;
};


drop temp;
save `saved'healstat, replace;

summ;
*drop _all;
cap log close ;
* compare summary stats before and after;
cd "$folder\hrs\merge\compare\" ;
summtab, contvars(_all) mean median range excel excelname(healstat.xlsx) replace;
cd "$folder\hrs\merge\" ;
use `saved'healstat.old.dta , clear;
cd "$folder\hrs\merge\compare\" ;
summtab, contvars(_all) mean median range excel excelname(healstat.xlsx) sheetname(Old);










